// pages/lose/lose.js
var Bmob = require('../../utils/Bmob-2.2.0.min.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isUser:true,
    lists:[]
  },
  detial:'',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
      that.detail=options.detail;
    
    that.getCodeStatus(that.detail);
  },

  /**
   * 获取标签状态
   */
getCodeStatus:function(objectId){
  var that=this;
  var userInfo = app.globalData.userInfo;
  var userId=userInfo.objectId;
  var message=Bmob.Query("good");

  if(typeof(objectId)=='undefined'){
    wx.showToast({
      title: '未找到码',
      icon: 'success',
      duration: 800,
      mask: true,
      success: function () {

        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../pages/page10000/page10000'
          })
        }, 1000)
      }
    })
    return;
  }
  if (objectId== '') {
    wx.showToast({
      title: '防丢签未找到',
      icon: 'success',
      duration: 900,
      success: function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../pages/page10000/page10000'
          })
        }, 1000)

      }
    })
    return;
  }
  message.get(objectId).then(res=>{
   
      if(res.status==-1){
       
      }

    if (res.status == 1) {
      that.getgoods();
      if(typeof(res.url)!='undefined'){
        that.getgoods();
        wx.redirectTo({
          url: res.url
        })
      }

      return;
      
    }

    if (res.status == 2) {
      if(userId==res.userInfo){
   

      }else{
        that.setData({
          isUser: false
        })

      }
      
       return;
    }
  }).catch(err=>{
    wx.showToast({
      title: '未找到',
      icon: 'success',
      duration: 800,
      success: function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../pages/page10000/page10000'
          })
        }, 1000)

      }
    })

  })

},


saveCodeInfo:function(){
  var that = this;
  var userInfo = app.globalData.userInfo;
  var userId = userInfo.objectId;
  var objectId = that.detail;
  var message = Bmob.Query("good");

  message.get(objectId).then(res => {
 
    if (res.status == -1) {
      res.set("status", 1);
      res.set("userInfo", userId);
      res.save().then(res => {
      
        wx.showToast({
          title: '标签设置完成哦',
          icon: 'success',
          duration: 900,
          success: function () {
            setTimeout(function () {
              //要延时执行的代码
              wx.switchTab({
                url: '../../pages/page10000/page10000'
              })
            }, 1000)

          }
        })
      })

    }

  

  }).catch(err => {
    wx.showToast({
      title: '未找到',
      icon: 'success',
      duration: 800,
      success: function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../pages/page10000/page10000'
          })
        }, 1000)

      }
    })

  })

},

getgoods:function(){
var that=this;
var goods = Bmob.Query("goods");
  goods.equalTo("state", "!=", 0);
  goods.limit(3);
  goods.find().then(res=>{

  that.setData({
    lists:res
  })
  })
},
  /**
     *微信通知来装车
     */
  notice: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var trucks = that.data.goodLists;
    var formId = trucks[index].formId;
    var openId = trucks[index].userInfo.authData.weapp.openid;
    var truckNumber = trucks[index].truckNumber;

    var noticeData = {
      "touser": openId,
      "template_id": 'JFHYA9I_k1fKiWg38CW47ofX9dXZ4V8gXst4sR2Z9jw',
      "page": "",
      "form_id": formId,
      "data": {
        "keyword1": {
          "value": truckNumber,
          "color": "#173177"
        },
        "keyword2": {
          "value": "有人捡到东西拉，快来查看联系方式"
        },

      }
      , "emphasis_keyword": ""

    }
    Bmob.sendWeAppMessage(noticeData).then(function (response) {
      console.log(response);
    }).catch(function (error) {
      console.log(error);
    });

    that.setTruckStatus(index, 2);

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });

  },

  hideInput: function () {
    var that = this;

    this.setData({
      inputVal: "",
      
      inputShowed: false,
      is_search: false
    });

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})